#!/bin/sh
main()
{
. $DOCUMENT_ROOT/apps/home/header.tpl.sh
header_html


cat <<'EOF'
<script>
function wan_connection_conn(wanzone)
{
if (confirm("你是想连接"+wanzone+"?")) {
    var data = "app=wan&action=do_wan_conn&wanzone=" + wanzone;
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'wan_setting');
	setTimeout("window.location.reload();", 5000);
  }
}
function wan_connection_disconn(wanzone)
{
if (confirm("你是想断开"+wanzone+"?")) {
    var data = "app=wan&action=do_wan_disconn&wanzone=" + wanzone;
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'wan_setting');
	setTimeout("window.location.reload();", 5000);
  }
}
</script>
EOF
cat <<EOF
		<div class="pure-g">
			<div class="l-box-lrg pure-u-1 pure-u-md-1-2">
			<h3>网络状态</h3>
EOF
main.sbin network_status
cat <<EOF
			</div>
			<div class="l-box-lrg pure-u-1 pure-u-md-1-2" id="wan_setting">
EOF
if
[ -n "$FORM_setting" ]
then
proto=$FORM_setting
else
proto=`main.sbin get_wan_proto`
fi
cat <<EOF
<h3>广域网网络设置</h3>
EOF

for wanzone in `$DOCUMENT_ROOT/apps/netzone/netzone.sbin list_all_wanzone`
do
proto=`uci get network.${wanzone}.proto`
cat <<EOF

<script>
function proto_change(wanzone,proto_id)
{
  var url = '/index.cgi?app=wan&action=post_select_proto&wanzone='+wanzone+'&proto='+\$('#'+proto_id).val();
  Ha.common.ajax(url, 'html', '', 'get', '', function(data){
    \$('#save_wan_${wanzone}').html(data);
  }, 1);
}
\$(function(){
  \$('#save_wan_${wanzone}').on('submit', function(e){
    e.preventDefault();
    var data = "app=wan&"+\$(this).serialize();
    var url = 'index.cgi';
    Ha.common.ajax(url, 'json', data, 'post', 'save_wan_${wanzone}');
  });
});
</script>
<h3>${wanzone}</h3>
协议:
<select id="proto_${wanzone}" onchange="proto_change('${wanzone}','proto_${wanzone}')">
	<option value="pppoe" `[ "$proto" = "pppoe" ] && echo selected`>宽带拨号</option>
	<option value="dhcp" `[ "$proto" = "dhcp" ] && echo selected`>动态地址获取</option>
	<option value="static" `[ "$proto" = "static" ] && echo selected`>静态地址</option>
</select>
<form id="save_wan_${wanzone}">
`post_select_proto`
</form>
EOF

done

# main.sbin "wan_setting_${proto}"

cat<<EOF
			</div>
		</div>
EOF

footer_html
}
post_select_proto()
{
[ -n "$FORM_wanzone" ] && wanzone=$FORM_wanzone
[ -n "$FORM_proto" ] && proto=$FORM_proto
setting_dhcp()
{
cat <<EOF

<table class="pure-table pure-table-bordered">
<tr>
<td>
dns
</td>
<td>
<input type="text" name="dns" value="`uci get network.${wanzone}.dns 2>/dev/null`">
</td>
</tr>
<tr>
<td>
macaddr
</td>
<td>
<input type="text" name="macaddr" value="`uci get network.${wanzone}.macaddr 2>/dev/null`">
</td>
</tr>
<tr>
<td colspan="2">
<input type="hidden" name="proto" value="dhcp">
<input type="hidden" name="action" value="post_save_wan">
<input type="hidden" name="wanzone" value="${wanzone}">
<button class="pure-button" type="submit">保存</button>
<button class="pure-button button-success" type="button" onclick="wan_connection_conn('${wanzone}')">连接</button>
<button class="pure-button button-error" type="button" onclick="wan_connection_disconn('${wanzone}')">断开</button>
</td>
</tr>
</table>
EOF
}
setting_pppoe()
{
cat <<EOF

<table class="pure-table pure-table-bordered">
<tr>
<td>
账号
</td>
<td>
<input type="text" name="username" value="`uci get network.${wanzone}.username 2>/dev/null`">
</td>
</tr>

<tr>
<td>
密码
</td>
<td>
<input type="password" name="password" value="`uci get network.${wanzone}.password 2>/dev/null`">
</td>
</tr>

<tr>
<td>
MTU
</td>
<td>
<input type="text" name="mtu" value="`uci get network.${wanzone}.mtu 2>/dev/null`">
</td>
</tr>

<tr>
<td>
MAC
</td>
<td>
<input type="text" name="macaddr" value="`uci get network.${wanzone}.macaddr 2>/dev/null`">
</td>
</tr>

<tr>
<td>
操作
</td>
<td>
<input type="hidden" name="proto" value="pppoe">
<input type="hidden" name="action" value="post_save_wan">
<input type="hidden" name="wanzone" value="${wanzone}">
<button class="pure-button" type="submit">保存</button>
<button class="pure-button button-success" type="button" onclick="wan_connection_conn('${wanzone}')">连接</button>
<button class="pure-button button-error" type="button" onclick="wan_connection_disconn('${wanzone}')">断开</button>
</td>
</tr>
</table>
EOF
}
setting_static()
{
cat <<EOF

<table class="pure-table pure-table-bordered">
<tr>
<td>
ipaddr
</td>
<td>
<input type="text" name="ipaddr" value="`uci get network.${wanzone}.ipaddr 2>/dev/null`">
</td>
</tr>

<tr>
<td>
netmask
</td>
<td>
<input type="text" name="netmask" value="`uci get network.${wanzone}.netmask 2>/dev/null`">
</td>
</tr>

<tr>
<td>
gateway
</td>
<td>
<input type="text" name="gateway" value="`uci get network.${wanzone}.gateway 2>/dev/null`">
</td>
</tr>

<tr>
<td>
dns
</td>
<td>
<input type="text" name="dns" value="`uci get network.${wanzone}.dns 2>/dev/null`">
</td>
</tr>

<tr>
<td>
macaddr
</td>
<td>
<input type="text" name="macaddr" value="`uci get network.${wanzone}.macaddr 2>/dev/null`">
</td>
</tr>

<tr>
<td>
操作
</td>
<td>
<input type="hidden" name="proto" value="static">
<input type="hidden" name="action" value="post_save_wan">
<input type="hidden" name="wanzone" value="${wanzone}">
<button class="pure-button" type="submit">保存</button>
<button class="pure-button button-success" type="button" onclick="wan_connection_conn('${wanzone}')">连接</button>
<button class="pure-button button-error" type="button" onclick="wan_connection_disconn('${wanzone}')">断开</button>
</td>
</tr>
</table>
EOF
}
setting_$proto
}
post_save_wan()
{
[ -n "$FORM_proto" ] && proto=$FORM_proto
if
[ "$proto" = "dhcp" ]
then
$(main.sbin www_root)/apps/wan/wan.sbin set_wan_dhcp
echo "设置成功!!" | main.sbin output_json 0
elif
[ "$proto" = "pppoe" ]
then
$(main.sbin www_root)/apps/wan/wan.sbin set_wan_pppoe
echo "设置成功!!" | main.sbin output_json 0
elif
[ "$proto" = "static" ]
then
$(main.sbin www_root)/apps/wan/wan.sbin set_wan_static
echo "设置成功!!" | main.sbin output_json 0
fi
}
do_wan_conn()
{
$(main.sbin www_root)/apps/wan/wan.sbin wan_conn
echo "连接成功!!" | main.sbin output_json 0
}
do_wan_disconn()
{
$(main.sbin www_root)/apps/wan/wan.sbin wan_disconn
echo "断开成功!!" | main.sbin output_json 0
}

lang=`main.sbin get_client_lang`
eval `cat $DOCUMENT_ROOT/apps/$FORM_app/i18n/$lang/i18n.conf`
if
[ $is_main_page = 1 ]
then
main
elif [ -n "$FORM_action" ]
then
$FORM_action
fi
